//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientExecuteDrop.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_player","_action_id","_arguments","_item_desc","_object_array","_pickedup_item","_obj_type","_creation_pos","_new_object","_player_objects","_array_index"
		,"_forindex","_object_index","_pickedup_objects","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = 	GETVAL(_arguments,TMF_EXECUTE_ACTION_PARAM_ACTID,-1);
_object = 		GETVAL(_arguments,TMF_EXECUTE_ACTION_PARAM_TARGET,objNull);

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
			
		_act_id = [_object,_action,"DROP_ACTION",-1] call FFUNC(actionGetLocal);
		if (_act_id >= 0) then {
			TRACE_1("Remove Drop Action",_act_id);
			player removeAction _act_id;
		};
		[_object,_action,"DROP_ACTION",-1] call FFUNC(actionSetLocal);

		_item_desc = [_parameters, "ITEM_DESC", typeof(_object)] call CFUNC(hashGet);
		_pickup_msg = [_parameters, "PICKUP_MSG", []] call CFUNC(hashGet);
		_shared_variable = [_parameters, "SHARED_VAR", ""] call CFUNC(hashGet);
		_initial_status = false; //[_parameters, "SHARED_VAR_VAL", false] call CFUNC(hashGet);
		_action_title = [_parameters, "PICKUP_TITLE", "STR_MF_PICKUP_DEFACTION"] call CFUNC(hashGet);
		_pickup_code = [_parameters, "PICKUP_CODE", ""] call CFUNC(hashGet);
		
		_act_id = [_object,_action,"PICKUP_ACTION",-1] call FFUNC(actionGetLocal);
		if (_act_id < 0) then {
			if (_action_title != "") then {
				_obj_var_picked = format["mission_object_pickedup_%1",_action_id];
				_action_title = (_action_title) call CFUNC(translate);
				_arguments = [];
				_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
				_action_display_timer = QGVAR(action_display_timer);
				_object setVariable [_action_display_timer,0];
				if (_initial_status) then {
					_condition = Format["(alive _target) && (_target getVariable '%1') && (_target getVariable '%2' < time)",_obj_var_picked,_action_display_timer];
				} else {
					_condition = Format["(alive _target) && !(_target getVariable '%1') && (_target getVariable '%2' < time)",_obj_var_picked,_action_display_timer];
				};
				_act_id = _object addAction [_action_title, TMF_ADD_ACTION_SCRIPT,_arguments,10,True,True,"",_condition];
				// We set the variable as local because it is dependent on the computer running it.
				[_object,_action,"PICKUP_ACTION",_act_id] call FFUNC(actionSetLocal);
				TRACE_1("Added Action",ARR_4(_act_id,_action_title,typeof _object,_condition));
			};
		};

		_object hideObject false;
		TRACE_1("Object now shown",ARR_2(typeof _object, _action_id));
	};
};

// _object = _this select 0; // Contains the player in this particular case, since he possesses the object
// _action = _this select 1;
// _parameters = _this select 2;
// _player = _this select 3;
// _action_id = _this select 4;
// _arguments = _this select 5; // Contain the action name and the reference of the object in the array

// //We make sure we only execute on the computer where the player launched the action
// if (_object == player) then {
	// _item_desc = _arguments select 1;
	// _object_array = objNull;
	// // Cannot drop what you don't have...
	// if (count d_mission_picked_up_items > 0) then {
		// for [{_i=0;},{_i < count d_mission_picked_up_items;},{_i = _i + 1;}] do {
			// _pickedup_item = d_mission_picked_up_items select _i;
			// if ((_pickedup_item select 0) == _item_desc) exitWith {
				// _object_array = _pickedup_item;
			// };
		// };
	
	// };	
	// if (!isNil "_object_array") then {
		// _obj_type = _object_array select 1;
		// _action = _object_array select 2;
		// _parameters = _object_array select 3;
		// _creation_pos = (getPos Player);
		// _creation_pos set [2, (_creation_pos select 2) + 0.5];
		// if (count _creation_pos > 0) then {
			// _new_object = createVehicle [_obj_type,_creation_pos,[],1,"CAN_COLLIDE"];
			// if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4 = createVehicle %5",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteDrop",_new_object,[_obj_type,_creation_pos,[],1,"CAN_COLLIDE"]];};
			// if (!isNull _new_object) then {
				// [_new_object,_action] call XfMissionSetAction;
				// [_new_object,_parameters] call XfMissionSetParameters;
				// if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4 - %5(%6)",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteDrop",(str _new_object),_action,count _parameters];};
				// _new_object call XfMissionAddObject;
				// ["d_all_mobject",_new_object] call XNetCallEvent;
				// ["d_msg",[Format[localize "STR_MF_DROPITEM",name player,_item_desc],"all",objNull,"hq"]] call XNetCallEvent;
				// player removeAction _action_id;
				// player reveal _new_object;
				// _player_objects = [];
				// _array_index = -1;
				// _forindex = 0;
				// _pickedup_objects = ["mission_pickedup_objects",[]] call XNetGetJIP;
				// if (count _pickedup_objects > 0) then {
					// {
						// if ((_x select 0) == str(_player)) exitWith {
							// _player_objects = _x select 1;
							// _array_index = _forindex;
						// };
						// _forindex = _forindex + 1;
					// } foreach _pickedup_objects;
				// };
				// diag_log Format["(%1)[%2]: %3: %4: %5",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteDrop","Player Objects",_player_objects];
				// if (_array_index >= 0) then {
					// if (isNil "_player_objects") then {_player_objects = []};
					// If (count _player_objects > 0) then {
						// _object_index = -1;
						// _forindex = 0;
						// {
							// diag_log Format["(%1)[%2]: %3: %4: %5",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteDrop","    Object ",_x];
							// if (((_x select 0) == _item_desc) and ((_x select 1) == _obj_type)) exitWith {
								// _object_index = _forindex;
							// };
							// _forindex = _forindex + 1;
						// } foreach _player_objects;
					// };
					// if (_object_index >= 0) then {
						// // The item is found, we remove it.
						// _player_objects set [_object_index, "xXxDELxXx"];
						// _player_objects = _player_objects - ["xXxDELxXx"];
					// } else {
						// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteDrop","The player doesn't have this object"];
					// };
					// // We update the array with the new player object list.
					// _pickedup_objects set [_array_index,[str(_player),_player_objects]];
					// ["mission_pickedup_objects",_pickedup_objects] call XNetSetJIP;
				// } else {
					// // The player has no object nothing to do
					// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteDrop","The player has no objects listed"];
				// };
				// if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: Picked up objects:%4",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteDrop",(["mission_pickedup_objects",[]] call XNetGetJIP)];};
			// } else {
				// localize "STR_MF_DROPITEMFAILED" call XfGlobalChat;
			// };
		// } else {
			// localize "STR_MF_DROPITEMFAILED" call XfGlobalChat;
		// };
	// } else {
		// localize "STR_MF_DROPITEMNOTFOUND" call XfGlobalChat;
		// player removeAction _action_id;
	// };
// };

TRACE("End");
TMFPROFILERSTOP;